<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;

class Code extends Model
{
    protected $table='tcode';

    public static $REGISTER=1;

    protected $fillable = ['ctype','CMobile','cnum'];

    //获取6位随机数
    public static function getRandNum()
    {
        return rand(1000,9999);
    }


    /**
     * 发送验证
     * @param array $input 'ctype','cmobile
     */
    public static  function sendSMS($input=array())
    {

        $num=self::getRandNum();

        $input=array_only($input,array('ctype','CMobile'));


        $code=self::where($input)->first();


        $input['cnum']=$num;

        if($code)
        {
            self::where('CID',$code->CID)->update($input);

        }else{
            self::create($input);
        }
        //发送验证码
        //测试
        echo $num;
    }

    /**
     * 检测验证码
     * @param array $input 'ctype','cmobile'，’num‘
     * @return bool
     */
    public static function Check($input=array())
    {
        $code=self::where(array_only($input,array('ctype','CMobile')))->first();

        if($code)
        {
            if($code->CNum==$input['cnum'])
            {
                return true;
            }else{
                return false;
            }
        }

        return false;
    }

}